<script>
  var countBattleships = function(board) {
    let count = 0;
    for(let i = 0; i < board.length; i++) {
      for(let j = 0; j < board[0].length; j++) {
        // 查找单元格是X的
        if (board[i][j] === 'X') {
          count++;
          // 查看上下左右方向是否有连着的，因为连着的就是同一个航母
          dfs(i, j)
        }
      }
    }

    function dfs(i, j){
      if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] !== 'X') {
        return;
      }

      // 把当前的单元格置成'.' 
      board[i][j] = '.';
      dfs(i - 1, j);
      dfs(i + 1, j);
      dfs(i, j - 1);
      dfs(i, j + 1);
    }

    return count;
  };

  console.log(countBattleships([["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]))
</script>